// 获取当前状态
chrome.tabs.query({ active: true }, (tabs) => {
  if (tabs.length > 0) {
    chrome.tabs.sendMessage(tabs[0].id, { command: "getStatus" }, (response) => {
      if (chrome.runtime.lastError) {
        console.log(chrome.runtime.lastError.message);
        return;
      }
      if (response) {
        statusChange(response.status);
      }
    });
  }
});

// 启动或暂停
document.getElementById('toggle-button').addEventListener('click', () => {
  chrome.tabs.query({active: true}, (tabs) => {
    chrome.tabs.sendMessage(tabs[0].id, {command: "toggleSelectable"}, (response) => {
      statusChange(response.status);
    });
  });
});


function statusChange(status) {
  let buttom = document.getElementById('toggle-button');
  buttom.classList.remove(status ? 'btn-primary' : "btn-success");
  buttom.classList.add(status ? 'btn-success' : "btn-primary");
  buttom.textContent = status ? "暂停" : "启动"; 
}